<template>
  <div class="clientadd">
    <van-nav-bar
      title="新增客户信息"
      right-text="首页"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <van-form @submit="onSubmit">
      <van-field required
        v-model="username"
        name="客户名称"
        label="客户名称"
        :rules="[{ required: true, message: '请填写客户名' }]"
      />
      <van-field required
        v-model="contact"
        name="联系方式"
        label="联系方式"
        :rules="[{ required: true, message: '请填写客户联系方式' }]"
        class="clientadd_one"
      />
      <van-field required
        v-model="education"
        name="学历"
        label="学历"
        :rules="[{ required: true, message: '请填写客户学历' }]"
      />
      <van-field required
        v-model="age"
        name="年龄"
        label="年龄"
        :rules="[{ required: true, message: '请填写客户年龄' }]"
      />
      <van-field required
        v-model="sex"
        name="性别"
        label="性别"
        :rules="[{ required: true, message: '请填写客户性别' }]"
        class="clientadd_border"
      />
      <van-field required
        v-model="city"
        name="所属城市"
        label="所属城市"
        :rules="[{ required: true, message: '请填写客户所属城市' }]"
      />
      <van-field required
        v-model="company"
        name="公司名称"
        label="公司名称"
        :rules="[{ required: true, message: '请填写客户公司名称' }]"
      />
      <van-field required
        v-model="duty"
        name="职务"
        label="职务"
        :rules="[{ required: true, message: '请填写客户公司名称' }]"
         class="clientadd_border"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" :disabled="ischeck" native-type="submit" @click="AddClick">提交</van-button>
      </div>
    </van-form>
  </div>
</template>
 
<script>
import {AddCust ,getCustId ,CompileCust} from "../../request/api"
export default {
  data() {
    return {
      username: "",
      contact:"",
      education:"",
        age:"",
        sex:"",
        city:"",
        company:"",
        duty:""
    };
  },
  computed:{
    ischeck(){
      // console.log();
      return !(this.username||this.contact||this.education||this.age||this.sex||this.city||this.company||this.duty)
    }
  },
  //编辑客户信息
  created(){
      this.$route.params.id
      if(this.$route.params.id){
          getCustId({
             id:this.$route.params.id
          }).then(res=>{
              console.log(res);
              let {custName,telephone,education,age,sex,cityName,company,position} = res.data
              this.username = custName
               this.contact = telephone
               this.education = education
               this.age = age
               this.sex = sex
               this.city = cityName
               this.company = company
               this.duty = position 
          })
      }else{
          console.log(2);
      }
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
    },
    onClickLeft() {
      this.$router.back();
    },
    onClickRight() {},
    AddClick(){
      if(this.$route.params.id){
        CompileCust({
         id: this.$route.params.id,
          custName:this.username,
            cityName:this.city,
            company:this.company,
            position:this.duty,
            telephone:this.contact,
            education:this.education,
            sex:this.sex,
            age:this.age
        }).then(res=>{
          console.log(res);
          if(res.errCode == 0){
            this.$router.push("/user")
          }
        })
        return
      }
        AddCust({
            custName:this.username,
            cityName:this.city,
            company:this.company,
            position:this.duty,
            telephone:this.contact,
            education:this.education,
            sex:this.sex,
            age:this.age
        }).then(res=>{
            console.log(res);
            if(res.errCode == 0){
                this.$router.push("/user")
            }
        })
    }
  },
};
</script>
 
<style lang = "less" scoped>
.van-nav-bar {
  margin-bottom: 10px;
  background: #369eff;
  /deep/.van-icon-arrow-left::before {
    color: #fff;
  }
  /deep/.van-nav-bar__text {
    color: #fff;
  }
  /deep/.van-nav-bar__title {
    color: #fff;
  }
}
.clientadd_one{
    margin-bottom: 15px;
}
.clientadd_border{
    border-top: 1px solid #ccc;
}
</style>